#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

pthread_once_t  once=PTHREAD_ONCE_INIT;

void    once_run(void)
{
	printf("once_run in thread %d\n",pthread_self());
}

void * child1(void *id)
{
	int tid=pthread_self();
    sleep(1);
	printf("thread %d[%d] enter\n",tid, *((int*)id));
	pthread_once(&once,once_run);
	printf("thread %d[%d] returns\n",tid, *((int*)id));
}

void * child2(void *id)
{
	int tid=pthread_self();
	printf("thread %d[%d] enter\n",tid, *((int*)id));
	pthread_once(&once,once_run);
	printf("thread %d[%d] returns\n",tid, *((int*)id));
}

int main(void)
{
	pthread_t tid1,tid2;
    int a = 1;
    int b = 2;
	printf("hello\n");
	pthread_create(&tid1,NULL,child1,&a);
	pthread_create(&tid2,NULL,child2,&b);
	sleep(5);
	printf("main thread exit\n");
	return 0;
}

